<template>
  <div>

    <div class="userinfo">
      <div class="userinfo-nickname btnm">
        {{userInfo.nickName}}
      </div>
      <img class="userinfo-avatar" :src="userInfo.avatarUrl" background-size="cover" />
    </div>
<div v-if="sysdisable">
  系统正在维护，暂停使用，稍后恢复，有问题请联系
</div>
    <div v-if="isGetUserInfoshow" class="btnm">
      <van-button type="primary" open-type="getUserInfo" @getuserinfo="getlogin" > 直接登陆订单系统（不使用微信登陆） </van-button>

      <div class="btnm">  <van-button type="primary" open-type="getUserInfo" @getuserinfo="getUserInfo" >登陆并提交您的微信信息 </van-button></div>
      <div style="text-align: center; line-height: 2.5">
        快速处理您的水果订单，带给您最好的水果
        
        <br><br>
        价格查询<br><br>
        <br><br>
        订单查询<br>
      </div>

    </div>



    <div class="announcementList">
      {{announcementText}}
    </div>

    <div v-if="isToolsShow&&!sysdisable">
      <div class="btnm">
        <van-button type="primary" icon="plus" @click="createOrder" block>创建新订单</van-button>
      </div>
      <div class="btnm">
        <van-button type="info" @click="historyOrder" block>新版历史订单</van-button>
      </div>
      <div class="btnm">
        <van-button type="info" @click="historyOrderOld" block>老版本历史订单</van-button>
      </div>
      <div class="btnm">
        <van-button type="default" @click="userexit" block>退出当前账户</van-button>
      </div>
    </div>




    <div v-if="isAdministratorsShow" class="btnm">
      <van-divider contentPosition="center">管理员操作区</van-divider>
      <div  class="btnm">
        <van-button style="margin-right: 12px" type="danger"  @click="getAdminUser" > 用户管理 </van-button>
        <van-button style="margin-right: 12px" type="danger"  @click="getAdminOrder"> 订单管理 </van-button>
        <van-button type="danger"  @click="getguoppage"> 库存品类 </van-button>
      </div>

      <div  class="btnm">
        <van-button style="margin-right: 12px" type="primary"  @click="getOrder" > 订单导出 </van-button>
        <van-button style="margin-right: 12px" type="info"  @click="getSysAdmin"> 系统管理 </van-button>
        <van-button style="margin-right: 12px" type="info"  @click="getAdminOrderOld"> 老单管理 </van-button>
      </div>

    </div>


  </div>
</template>

<script>
  import {announcement_list_one,sysInfo} from '../../api/basisMG'

export default {
  data () {
    return {
      sysdisable:false,
      announcementText:"",
      isAdministratorsShow:false,
      isUserinfoshow:false,
      isGetUserInfoshow:false,
      isToolsShow:false,
      userInfo: {
        nickName: '',
        avatarUrl: ''
      },
      loginInfo:{}
    }
  },

  // onShow() {
  //   let userInfo = wx.getStorageSync('userInfo')
  // },

  beforeMount(){

    let t = this
    this.loginInfo = wx.getStorageSync('loginInfo')
    //let openid = wx.getStorageSync('openid')||""
    let userInfo = wx.getStorageSync('userInfo')


    //判断是否是管理员
    let a = t.globalData.administrators.filter(function(val,key) {
      if( val == t.loginInfo.username){
        return val
      }
    })

    if(a.length){
      this.isAdministratorsShow = true
    }else {
      this.isAdministratorsShow = false

    }

    this.loadsysInfo()

    //
    // //获取openid
    // if(!openid){
    //   wx.cloud.callFunction({ name: 'login' }).then(res => {
    //     wx.setStorageSync('openid', res.result.openid)
    //   })
    // }



    //获取用户微信信息 userInfo

    console.log(userInfo.userInfo)
    if(userInfo.userInfo){
      this.isUserinfoshow = true
      this.userInfo = userInfo.userInfo
      console.log(userInfo.userInfo)
      if(t.loginInfo.username){
        this.isToolsShow = true
        this.isGetUserInfoshow = false
      }
      this.isGetUserInfoshow = false

    }else {
      console.log("没有缓存userinfo")
      if(t.loginInfo.username){
        this.isToolsShow = true
        this.isGetUserInfoshow = false

      }else{
        this.isGetUserInfoshow =true
        return false
      }

    }



    //获取登录信息loginInfos

    if(t.loginInfo.username){
      console.log("之前登陆过")
      t.isToolsShow = true
    }else{
      console.log("需要登陆1")
      wx.navigateTo({
        url: "/pages/login/main"
      })
    }


    this.loadannouncementText()

    },


  methods: {

    loadsysInfo(){
      sysInfo().then(res => {
        if(res.data.disable){

          if(!this.isAdministratorsShow){
            this.sysdisable = res.data.disable
          }
        }
      })

    },

    loadannouncementText(){
      announcement_list_one().then(res => {
          this.announcementText = res.data.text
        })

        },

    getSysAdmin(){
      wx.navigateTo({
        url: "/pages/options/main"
      })
    },


    getlogin(){
      wx.navigateTo({
        url: "/pages/login/main"
      })
    },

    getOrder(){
      wx.navigateTo({
        url: "/pages/manageOrder_Export/main"
      })
    },

    getguoppage(){
      wx.navigateTo({
        url: "/pages/options_guopin/main"
      })
    },

    getOptions(){
      wx.navigateTo({
        url: "/pages/options/main"
      })
    },

    getAdminUser(){
      console.log(111)
      wx.navigateTo({
        url: "/pages/manageUser/main"
      })
    },
    getAdminOrder(){

      wx.navigateTo({
        url: "/pages/manageOrder/main"
      })
    },

    getAdminOrderOld(){

      wx.navigateTo({
        url: "/pages/manageOrder_Old/main"
      })
    },

    getUserInfo(data){
      let t = this;
      if(data.mp.detail.rawData){
        wx.getUserInfo({
          success:(data)=>{
            console.log(data)
            this.userInfo = data.userInfo
            wx.setStorageSync('userInfo', data)
            //获取用户信息之间检测 是否登陆
            if(t.loginInfo.username){
              this.isToolsShow = true
              this.isGetUserInfoshow = false
            }else{
              wx.navigateTo({
                url: "/pages/login/main"
              })
            }

          }
        })
      }
    },
    createOrder(data){
      wx.navigateTo({
        url: "/pages/order_add/main"
      })
    },
    historyOrder(data){
      wx.navigateTo({
        url: "/pages/order/main"
      })
    },

    historyOrderOld(data){
      wx.navigateTo({
        url: "/pages/order_old/main"
      })
    },
    userexit(){
        wx.removeStorage({
          key: 'loginInfo',
        })
        // wx.removeStorage({
        //   key: 'openid',
        // })
        wx.removeStorage({
          key: 'userInfo',
        })
        console.log("退出登录")
        wx.navigateTo({
          url: "/pages/login/main"
        })
    }
  },
}
</script>

<style >

  page{
    background-image: url("http://39.101.138.78:25811/img/indexbg.jpg");
    background-size: 100%;
    background-repeat: no-repeat;
    background-position: top;
    padding-top: 550rpx;
  }
.btnm{padding: 32rpx; text-align: center}


.userinfo {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.userinfo-avatar {
  width: 128rpx;
  height: 128rpx;
  margin: 20rpx;
  border-radius: 50%;
}

.userinfo-nickname {
  color: #aaa;
}

.usermotto {
  margin-top: 150px;
}

.form-control {
  display: block;
  padding: 0 12px;
  margin-bottom: 5px;
  border: 1px solid #ccc;
}
.all{
  width:7.5rem;
  height:1rem;
  background-color:blue;
}
.all:after{
  display:block;
  content:'';
  clear:both;
}
.left{
  float:left;
  width:3rem;
  height:1rem;
  background-color:red;
}

.right{
  float:left;
  width:4.5rem;
  height:1rem;
  background-color:green;
}
  .announcementList{ text-align: center;padding: 20px}
</style>
